<?php
/**
 * [woniu] Copyright (c) 2016/8/18
 * success.ctrl
 * 支付成功控制器
 */
defined('IN_IA') or exit('Access Denied');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';

$_W['page']['title'] = '支付结果';//这里的标题借用系统全局变量解决外部方法获取不到标题的问题
if($op =='display'){
	fx_message('支付成功<br>实付:<em>'.$_GPC['payprice'].'</em>元', app_url('records/records/list'), 'success');
}
if($op =='delivery'){
	if(empty($_SESSION['pay']))fx_message("页面过期，请刷新页面", '', 'error');
	$params = @json_decode(base64_decode($_GPC['params']), true);
	$records = pdo_get('fx_activity_records', array('orderno' => $params['tid']));
	//验证名额，每次进入加载剩余名额数量、及规格
	$activity = model_activity::getSingleActivity($_SESSION['pay']['activityid'], '*');
	if ($activity['hasoption']){//规格选项
		$option = model_activity::getSingleActivityOption($records['optionid']);
		$activity['falsenum'] = $option['falsenum'] ? $option['falsenum'] : 0;
	}
	$quota = $option['quota']?$option['quota']:$activity['quota'];
	if ($quota > 0){
		$joinnum = model_records::getJoinNum($activity['id'], $records['optionid']) + $activity['falsenum'];
		if($joinnum >= $quota) {
			fx_message("很遗憾！名额已经满了", '', 'error');
		}elseif($joinnum + $_SESSION['pay']['teamnum'] > $quota){
			fx_message("当前活动仅剩 " . ($quota - $joinnum) . ' 个名额', '', 'error');
		}
	}
	//保存修改信息
	//$listNum = pdo_fetchall("SELECT hexiaoma as num FROM " . tablename ('fx_activity_records') . " WHERE activityid = ".$activity['id']);
	//$hexiaoma = createNumber($listNum);
	$hexiaoma = createRandomNumber(8);
	$data = array('paytype' => $op,'hexiaoma'=>$hexiaoma);
	$result = pdo_update('fx_activity_records', $data, array('orderno' => $params['tid'], 'uniacid' => $_W['uniacid']));
	if ($result){
		//二维码
		createQrcode::creategroupQrcode($params['tid']);
		$url = app_url('records/records/list'); // 报名成功通知
		message::join_success($_W['openid'], $activity, $records['id'], $url);		
		if($activity['smsswitch']){//发送短信
			$smsparams=array(
				'product' => $_W['_config']['sname'],
				'item'    => $activity['title'],
				'name'    => $records['realname'],
				'timestr' => date('m月d日 H:i',strtotime($activity['starttime'])),
				'idcode'  => $data['hexiaoma']
			);
			sendSMS($records['mobile'],$smsparams,$activity['smsnotify']);
		}
		$recordid = $records['id'];
		if ($_W['_config']['admin_notice']){//管理员通知
			if ($activity['merchantid'])
			$merchant = model_merchant::getSingleMerchant($activity['merchantid'], '*');//读取主办方
			$openids = unserialize($activity['openids']);
			$openids = !empty($openids) ? $openids : unserialize($merchant['messageopenid']);
			$openids = !empty($openids) ? $openids : $_W['_config']['openids'];
			if (!empty($openids)){
				foreach($openids as $key=> $value){
					message::admin_notice($value, $activity, $recordid, '');
				}
			}
		}
	}
	$tmplmsg = unserialize($activity['tmplmsg']);
	$remark  = empty($tmplmsg['joinremark'])?$activity['title']:$tmplmsg['joinremark'];
	fx_message('恭喜，你已报名成功<br>付款方式：<em>线下付款</em>', app_url('records/records/list'), 'success', $remark);
}
if($op =='activity_ajax'){

}